package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.JSONObject1O;
import com.ruoyi.common.utils.StrKit;
import com.ruoyi.system.domain.ListDetail;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrderList;
import com.ruoyi.system.service.IOrderListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 * 
 * @author ruoyi
 * @date 2025-08-15
 */
@RestController
@RequestMapping("/system/orderlist")
public class OrderListController extends BaseController
{
    @Autowired
    private IOrderListService orderListService;

    /**
     * 确认支付订单
     * @param ids
     * @return
     */
    @PostMapping("/checkoutOrder/{ids}")
    @ResponseBody
    public AjaxResult checkoutOrder(@PathVariable("ids")long[] ids){
        StringBuffer sb=new StringBuffer("(");
        for(int i=0;i<ids.length;i++){
            if(i!=0){
                sb.append(",");
            }
            sb.append("'").append(ids[i]).append("'");
        }
        sb.append(")");
        String idsStr=sb.toString();
        System.out.println("确认收货ID："+idsStr);

        AjaxResult ajaxResult=orderListService.checkoutOrder(idsStr);
        return ajaxResult;
    }

    /**
     * 评价订单
     */
    @RequestMapping("/evaluate")
    public AjaxResult evaluate(@RequestBody OrderList orderList)
    {
        AjaxResult ajaxResult = orderListService.evaluate(orderList);
        return ajaxResult;
    }

    /**
     * 确认收货
     * @param jsonObject
     * @return
     */
    @RequestMapping("/receiveOrder")
    public AjaxResult receiveOrder(@RequestBody JSONObject jsonObject)
    {
        System.out.println("确认收货："+jsonObject.toString());

        AjaxResult ajaxResult = orderListService.receiveOrder(jsonObject);
        return ajaxResult;
    }

    /**
     * 修改订单商品信息
     */
    @RequestMapping("/updateOrderGoods")
    public AjaxResult updateOrderGoods(@RequestBody JSONObject jsonObject)
    {
        AjaxResult ajaxResult = orderListService.updateOrderGoods(jsonObject);
        return ajaxResult;
    }
    /**
     * 查询采购单列表
     */
    @RequestMapping("/getListGoods")
    public TableDataInfo getListGoods(@RequestParam("id")long id)
    {
        List<ListDetail> data = orderListService.getListGoods(id);
        return getDataTable(data);
    }

    /**
     * 查询采购单列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list")
    public TableDataInfo list(OrderList orderList,@RequestParam(name="noPage",required = false) String noPage)
    {
        if(StrKit.notBlank(noPage)){

        }else{
            startPage();
        }
        List<OrderList> list = orderListService.selectOrderListList(orderList);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderList orderList)
    {
        List<OrderList> list = orderListService.selectOrderListList(orderList);
        ExcelUtil<OrderList> util = new ExcelUtil<OrderList>(OrderList.class);
        util.exportExcel(response, list, "采购单数据");
    }

    /**
     * 获取采购单详细信息
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderListService.selectOrderListById(id));
    }

    /**
     * 新增采购单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject data)
    {
        String username = getLoginUser().getUser().getNickName();
        data.put("userName", username);
//        System.out.println("新增采购单："+data.toString());
        return toAjax(orderListService.insertOrderList(data));
    }

    /**
     * 修改采购单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderList orderList)
    {
        return toAjax(orderListService.updateOrderList(orderList));
    }

    /**
     * 删除采购单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderListService.deleteOrderListByIds(ids));
    }
}
